<template>
  <div class="update-container">
    <div class="title-container">
      <h3 class="title">修改密码</h3>
    </div>
    <div class="content">

      <div style="height:300px">
        <el-steps :active="active">
          <el-step title="步骤 1" description="确认账号" />
          <el-step title="步骤 2" description="校验验证码" />
          <el-step title="步骤 3" description="修改密码" />
        </el-steps>
        <div style="width:500px;">

          <el-form ref="ruleForm" style="margin-top:40px;margin-left:150px" label-position="left" :model="ruleForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
            <el-form-item v-if="active===1" label="账号" prop="userName">
              <el-input v-model="ruleForm.userName" type="userName" autocomplete="off" />
            </el-form-item>
            <el-form-item v-if="active===1" label="密码" prop="passWord">
              <el-input v-model="ruleForm.passWord" type="passWord" autocomplete="off" />
            </el-form-item>

            <el-form-item v-if="active===2" label="手机号" prop="phone">
              <el-input v-model="ruleForm.phone" autocomplete="off" />
            </el-form-item>
            <el-form-item v-if="active===2" label="验证码" prop="code">
              <el-input v-model="ruleForm.code" autocomplete="off" />
            </el-form-item>

            <el-form-item v-if="active===3" label="新密码" prop="pass">
              <el-input v-model="ruleForm.pass" type="passWord" autocomplete="off" />
            </el-form-item>
            <el-form-item v-if="active===3" label="确认新密码" prop="checkPass">
              <el-input v-model="ruleForm.checkPass" type="passWord" autocomplete="off" />
            </el-form-item>
            <el-button v-if="active===2" :disabled="codeBtnDisabled" style="float:right" type="primary" @click="handleCodeBtn">{{ codeBtn }}</el-button>
          </el-form>

        </div>
      </div>
      <div style="display:flex;justify-content:flex-end">
        <el-button type="primary" @click="active!==1?active -=1:''">上一步</el-button>
        <el-button v-if="active!==3" type="primary" @click="handleStep()">下一步</el-button>
        <el-button v-if="active===3" type="primary" @click="submitForm('ruleForm')">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getVerifyCode, updatePassword, verifyCode, verifyUsernamePassword } from '@/api/user'

export default {
  name: 'UpdatePassword',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      codeUUID: '',
      codeBtn: '获取验证码',
      codeBtnDisabled: false,
      active: 1,
      ruleForm: {
        userName: '',
        passWord: '',
        code: '',
        phone: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        userName: [
          { required: true, trigger: 'blur' }
        ],
        passWord: [
          { required: true, trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    tackBtn() { // 验证码倒数30秒
      let time = 30
      const timer = setInterval(() => {
        if (time === 0) {
          clearInterval(timer)
          this.codeBtn = '获取验证码'
          this.codeBtnDisabled = false
        } else {
          this.codeBtn = time + '秒后重试'
          this.codeBtnDisabled = true
          time--
        }
      }, 1000)
    },
    handleCodeBtn() {
      this.tackBtn()
      const { userName, passWord } = this.ruleForm
      getVerifyCode(userName, passWord).then((res) => {
        this.codeUUID = res
      })
    },
    handleStep() {
      const { userName, passWord } = this.ruleForm
      if (this.active === 1) {
        // 这里需要验证账号密码
        verifyUsernamePassword(userName, passWord).then(() => {
          if (this.active !== 3) this.active += 1
        })
      } else if (this.active === 2) {
        // 验证验证码
        const { codeUUID } = this
        const { code } = this.ruleForm
        verifyCode(codeUUID, code).then(() => {
          if (this.active !== 3) this.active += 1
        })
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const { userName, passWord, pass, code } = this.ruleForm
          const { codeUUID } = this
          const form = { userName, passWord, newPassWord: pass, code, uuid: codeUUID }
          updatePassword(form).then(() => {
            this.$message.success('修改密码成功')
            this.$router.push({ path: '/login' })
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.update-container{
    background: url(~@/assets/images/login.jpg);
    background-size: 100% 100%;
    height:100%;
    width:100%;
    display: flex;
    align-items:center;
    justify-content:center;
    flex-direction: column;
}

.content{
  width:700px;
}

.title-container {
    position: relative;
    color: #454545;

    .title {
      font-size: 26px;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
</style>

